package com.zhouxiaoge.memcached;

import net.spy.memcached.CASResponse;
import net.spy.memcached.CASValue;
import net.spy.memcached.MemcachedClient;
import net.spy.memcached.internal.OperationFuture;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Future;

/**
 * @author 周明阳
 * email: gqzmy@outlook.com
 * Date: 2018-07-18 15:29
 */

public class SpyMemcachedJava {
    public static void main(String[] args) {
        try {
            /*本地链接Memcached服务*/
            MemcachedClient mc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
            /*+++++++++++++++++++++++++++++++++++++set操作++++++++++++++++++++++++++++++++++++++++++++*/
            /*存储数据*/
            Future fo = mc.set("zhouxiaoge", 900, "周小哥");
            System.out.println("set的操作的状态: " + fo.get());
            System.out.println("set的值: " + mc.get("zhouxiaoge"));
            /*+++++++++++++++++++++++++++++++++++++add操作++++++++++++++++++++++++++++++++++++++++++++*/
            /*添加*/
            OperationFuture<Boolean> add = mc.add("zhouxiaoge", 9000, "memcached");
            System.out.println("有旧key的添加操作: " + add.get());
            /*添加新key*/
            OperationFuture<Boolean> newAdd = mc.add("newAdd", 900, "All in AI !!!");
            System.out.println("无旧key的添加操作:　" + newAdd.get());
            System.out.println("新key的value值:　" + mc.get("newAdd"));
            /*+++++++++++++++++++++++++++++++++++++delete操作++++++++++++++++++++++++++++++++++++++++++++*/
            OperationFuture<Boolean> delete = mc.delete("newAdd");
            System.out.println("删除执行的结果: " + delete.get());
            System.out.println("获取删除后key的value值: " + mc.get("newAdd"));
            /*+++++++++++++++++++++++++++++++++++++replace操作++++++++++++++++++++++++++++++++++++++++++++*/
            /*替换操作*/
            OperationFuture<Boolean> replace = mc.replace("zhouxiaoge", 900, "村头南霸天");
            System.out.println("有旧值的替换操作: " + replace.get());
            System.out.println("有旧值的替换操作，操作后值为:　" + mc.get("zhouxiaoge"));
            OperationFuture<Boolean> replace1 = mc.replace("newReplace", 900, "这是一个没有旧值的替换操作");
            System.out.println("没有旧值的替换操作: " + replace1.get());
            System.out.println("没有旧值的替换操作，操作后值为:　" + mc.get("newReplace"));
            mc.delete("newReplace");
            /*+++++++++++++++++++++++++++++++++++++append操作++++++++++++++++++++++++++++++++++++++++++++*/
            /*对存在的key进行数据添加操作*/
            OperationFuture<Boolean> append = mc.append("zhouxiaoge", UUID.randomUUID().toString().replace("-", ""));
            System.out.println("对存在的key值进行append操作: " + append.get());
            System.out.println("append操作后的key的值: " + mc.get("zhouxiaoge"));
            /*+++++++++++++++++++++++++++++++++++++prepend操作++++++++++++++++++++++++++++++++++++++++++++*/
            OperationFuture<Boolean> prepend = mc.prepend("zhouxiaoge", "zhouxiago-prepend");
            System.out.println("对存在的key的值进行prepend操作: " + prepend.get());
            System.out.println("对存在的key的值进行prepend操作: " + mc.get("zhouxiaoge"));
            //mc.delete("zhouxiaoge");
            /*+++++++++++++++++++++++++++++++++++++gets操作CAS实例++++++++++++++++++++++++++++++++++++++++++++*/
            /*通过gets方法获取CAS token令牌*/
            CASValue casValue = mc.gets("zhouxiaoge");
            System.out.println("CAS token令牌: " + casValue);
            /*使用CAS方法来更新数据*/
            CASResponse casResponse = mc.cas("zhouxiaoge", casValue.getCas(), 900, "casValue");
            System.out.println(casResponse);
            System.out.println(mc.get("zhouxiaoge"));
            /*+++++++++++++++++++++++++++++++++++++CAS操作++++++++++++++++++++++++++++++++++++++++++++*/
            /*使用get方法获取数据*/
            Object get = mc.get("zhouxiaoge");
            System.out.println(get);
            /*+++++++++++++++++++++++++++++++++++++Incr/Decr操作++++++++++++++++++++++++++++++++++++++++++++*/
            mc.set("number", 900, "10");
            System.out.println("number的值: " + mc.get("number"));
            System.out.println("自增111后的值: " + mc.incr("number", 111));
            System.out.println("自减112后的值: " + mc.decr("number", 112));
            /*+++++++++++++++++++++++++++++++++++++Memcached的status++++++++++++++++++++++++++++++++++++++++++++*/
            System.out.println("==========================Memcached的status===============================");
            Map<SocketAddress, Map<String, String>> stats = mc.getStats();
            for (SocketAddress socketAddress : stats.keySet()) {
                System.out.println("socketAddress : " + socketAddress);
                Map<String, String> stringStringMap = stats.get(socketAddress);
                for (String s : stringStringMap.keySet()) {
                    System.out.println(s + " : " + stringStringMap.get(s));
                }
            }
            /*+++++++++++++++++++++++++++++++++++++Memcached的items++++++++++++++++++++++++++++++++++++++++++++*/
            System.out.println("==========================Memcached的items===============================");
            Map<SocketAddress, Map<String, String>> items = mc.getStats("items");
            for (SocketAddress socketAddress : items.keySet()) {
                System.out.println("socketAddress : " + socketAddress);
                Map<String, String> stringStringMap = items.get(socketAddress);
                for (String s : stringStringMap.keySet()) {
                    System.out.println(s + " : " + stringStringMap.get(s));
                }
            }
            /*+++++++++++++++++++++++++++++++++++++Memcached的slabs++++++++++++++++++++++++++++++++++++++++++++*/
            System.out.println("==========================Memcached的slabs===============================");
            Map<SocketAddress, Map<String, String>> slabs = mc.getStats("slabs");
            for (SocketAddress socketAddress : slabs.keySet()) {
                System.out.println("socketAddress : " + socketAddress);
                Map<String, String> stringStringMap = slabs.get(socketAddress);
                for (String s : stringStringMap.keySet()) {
                    System.out.println(s + " : " + stringStringMap.get(s));
                }
            }
            /*+++++++++++++++++++++++++++++++++++++Memcached的sizes++++++++++++++++++++++++++++++++++++++++++++*/
            System.out.println("==========================Memcached的sizes===============================");
            Map<SocketAddress, Map<String, String>> sizes = mc.getStats("sizes");
            for (SocketAddress socketAddress : slabs.keySet()) {
                System.out.println("socketAddress : " + socketAddress);
                Map<String, String> stringStringMap = sizes.get(socketAddress);
                for (String s : stringStringMap.keySet()) {
                    System.out.println(s + " : " + stringStringMap.get(s));
                }
            }
            System.out.println("==========================flush_all===============================");
            System.out.println("zhouxiaoge的Value: " + mc.get("zhouxiaoge"));
            System.out.println("number的Value: " + mc.get("number"));
            //OperationFuture<Boolean> flush = mc.flush();
            OperationFuture<Boolean> flush = mc.flush(1);
            Thread.sleep(10000);
            System.out.println("flush的操作状态: " + flush.get());
            System.out.println("flush后的zhouxiaoge的Value: " + mc.get("zhouxiaoge"));
            System.out.println("flush后的number的Value: " + mc.get("number"));
            /*关闭链接*/
            mc.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
